package com.wn.main.ui.other

import android.content.Context
import android.content.Intent
import android.net.Uri
import com.tencent.smtt.export.external.interfaces.SslError
import com.tencent.smtt.export.external.interfaces.SslErrorHandler
import com.tencent.smtt.sdk.ValueCallback
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient
import com.wn.common.constant.KEY_TITLE
import com.wn.common.constant.KEY_URL
import com.wn.framework.base.BaseActivity
import com.wn.main.R
import com.wn.main.databinding.ActivityArticleDetailBinding

/**
 * @Author: wn
 * @Date: 2024/12/5 16:05
 * @Description: 文字详情页
 */
class ArticleDetailActivity : BaseActivity<ActivityArticleDetailBinding>() {

  override fun getLayoutRes() = R.layout.activity_article_detail

  private var mTitle: String? = ""
  private var mUrl: String? = ""

  // 取代 onNewIntent 获取 intent 数据
  override fun initData() {
    intent?.apply {
      mTitle = getStringExtra(KEY_TITLE)
      mUrl = getStringExtra(KEY_URL)
    }
  }

  override fun initView() {
    // 加载 webView
    initWebView()
  }

  /**
   * 加载 webView
   */
  private fun initWebView() {
    showLoading()
    mBinding.webView.apply {
      webChromeClient = mWebChromeClient
      webViewClient = mWebViewClient // 如果不设置则跳系统浏览器
      loadUrl(mUrl)
    }
  }

  /**
   * 辅助 WebView 处理 Javascript 的对话框,网站图标,网站标题等等
   */
  private val mWebChromeClient = object : WebChromeClient() {
    /**
     * 网页 Title更改
     */
    override fun onReceivedTitle(webView: WebView?, title: String?) {
      super.onReceivedTitle(webView, title)
      if (webView != null && !webView.canGoBack() && !mTitle.isNullOrEmpty()) {
        mBinding.titleBar.setMiddleText(mTitle)
      } else {
        var titleResult = ""
        if (!title.isNullOrEmpty() && !title.startsWith("http", true)) {
          titleResult = title
        }
        mBinding.titleBar.setMiddleText(titleResult)
      }
    }

    /**
     * 文件选择回调
     */
    override fun onShowFileChooser(
      webView: WebView?,
      back: ValueCallback<Array<Uri>>?,
      chooser: FileChooserParams?,
    ): Boolean {
      return super.onShowFileChooser(webView, back, chooser)
    }

    /**
     * 获得网页的加载进度并显示 取消进度框
     */
    override fun onProgressChanged(webView: WebView?, process: Int) {
      super.onProgressChanged(webView, process)
    }
  }

  /**
   * 处理各种通知 & 请求事件
   */
  private val mWebViewClient = object : WebViewClient() {
    /**
     * 网页加载完毕
     */
    override fun onPageFinished(webView: WebView?, url: String?) {
      super.onPageFinished(webView, url)
      dismissLoading()
    }

    /**
     * 跳转拦截处理
     * 打开网页时，不调用系统浏览器进行打开，而是在本 WebView中 直接显示
     */
    override fun shouldOverrideUrlLoading(webview: WebView?, url: String?): Boolean {
      // 处理url
      // mBinding.webView.loadUrl(url)
      return super.shouldOverrideUrlLoading(webview, url)
    }

    /**
     * 设置错误页面
     */
    override fun onReceivedError(webview: WebView?, p1: Int, p2: String?, p3: String?) {
      super.onReceivedError(webview, p1, p2, p3)
    }

    override fun onReceivedSslError(
      webView: WebView?,
      handler: SslErrorHandler?,
      error: SslError?,
    ) {
      super.onReceivedSslError(webView, handler, error)
      // 忽略ssl错误
      handler?.proceed()
    }
  }

  @Deprecated(
    "Deprecated in Java",
    ReplaceWith("super.onBackPressed()", "com.wn.framework.base.BaseActivity")
  )
  override fun onBackPressed() {
    if (mBinding.webView.canGoBack()) {
      mBinding.webView.goBack() // 返回上一个浏览页
    } else {
      super.onBackPressed()
    }
  }

  companion object {
    fun start(context: Context, url: String, title: String) {
      Intent(context, ArticleDetailActivity::class.java).apply {
        putExtra(KEY_URL, url)
        putExtra(KEY_TITLE, title)
        context.startActivity(this)
      }
    }
  }
}